/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class UserPresenter {
	private ClientUser currentUser = new ClientUser();
	private MainPresenter mainPresenter;
	private QurTextServiceAsync qurTextService;
	private HeaderView headerView;
	
	public HeaderView getHeaderView() {
		return headerView;
	}

	public void setHeaderView(HeaderView headerView) {
		this.headerView = headerView;
	}

	public QurTextServiceAsync getQurTextService() {
		return qurTextService;
	}

	public void setQurTextService(QurTextServiceAsync qurTextService) {
		this.qurTextService = qurTextService;
	}

	public void setCurrentUser(ClientUser currentUser) {
		this.currentUser = currentUser;
	}

	public ClientUser getCurrentUser() {
		return currentUser;
	}

	public void onLoginLinkClick() {
		if (null==currentUser.loginUrl) return;
		mainPresenter.redirectUrl(currentUser.loginUrl);
	}

	public void setMainPresenter(MainPresenter mainPresenter) {
		this.mainPresenter = mainPresenter;
	}

	public MainPresenter getMainPresenter() {
		return mainPresenter;
	}

	public void processUserProgress(final ClientSection section) {
		getCurrentUser().sectionRead[section.sectionNo]=true;
		getCurrentUser().lastSection=mainPresenter.getHistoryToken();
		saveProgress();
		showUserProgress();
	}

	private void saveProgress() {
		if (getCurrentUser().isLoggedIn()) {
			qurTextService.saveProgress(getCurrentUser(), new AsyncCallback<Void>(){

				@Override
				public void onFailure(Throwable caught) {
					mainPresenter.handleError(caught);
				}

				@Override
				public void onSuccess(Void result) {
					//do nothing
				}
				
			});
		} else {
			mainPresenter.saveProgressCookie(getCurrentUser().getSectionReadCookie(), mainPresenter.getHistoryToken());
		}
	}

	private void showUserProgress() {
		String userProgressText = "" + (getCurrentUser().getSectionCount()*1000/558)/10.0 + "% Completed | ";
		mainPresenter.getNavigationPresenter().showUserProgress();
		headerView.setUserProgressText(userProgressText);
	}

	public void onUserPresenterLoad() {
	    qurTextService.getCurrentUser(mainPresenter.getCurrentUrl(), new AsyncCallback<ClientUser>() {

			@Override
			public void onFailure(Throwable caught) {
				mainPresenter.handleError(caught);
			}

			@Override
			public void onSuccess(ClientUser user) {
				setCurrentUser(user);
				if (getCurrentUser().isLoggedIn()) {
					headerView.setUserText(user.email);
					headerView.setLoginLinkText("Log out");
					headerView.setLoginLinkHint("stop tracking the progress of user " + user.email);
				    if (user.isAdmin) {
				    	mainPresenter.showAdminTab();
				    }
				} else {
					headerView.setUserText("Guest");
					headerView.setLoginLinkText("Log in/Register");
					headerView.setLoginLinkHint("track your own progress (on a shared or multiple computers).");
				}
				loadProgress();
				showUserProgress();
				if ("".equals(mainPresenter.getHistoryToken())) mainPresenter.newHistoryItem(mainPresenter.getUserPresenter().getCurrentUser().lastSection);
				mainPresenter.handleHistoryToken(mainPresenter.getHistoryToken());
			}

	    });
	}

	private void loadProgress() {
		if (getCurrentUser().isLoggedIn()) {
			if (getCurrentUser().getSectionCount()==0) { //new user;
				getCurrentUser().setSectionReadCookie(mainPresenter.getCookie("read"));
			}
			if (null==getCurrentUser().lastSection) {
				getCurrentUser().lastSection=mainPresenter.getCookie("last");
			}
		} else {
			getCurrentUser().setSectionReadCookie(mainPresenter.getCookie("read"));
			getCurrentUser().lastSection=mainPresenter.getCookie("last");
		}
	}

	public void resetUserProgress() {
		getCurrentUser().sectionRead=new boolean[558];
		processUserProgress(mainPresenter.getCurrentSection());
	}

}
